/*
 * @Author: zs.duan
 * @Date: 2023-05-01 18:08:44
 * @LastEditors: zs.duan
 * @LastEditTime: 2023-11-10 22:34:05
 * @FilePath: \serve\routes\adminApi.js
 */
var express = require("express");
var router = express.Router();
const Db = require("../utils/db");
const createSql = require("../utils/Create_SQL");
const path = require('path');
const { errorCode, successCode, config } = require('../config/config');
const utils = require('../utils/utils.js');
const objectCheck = require("./adminApiCheck.js");
const nodeExcel = require('excel-export'); //处理 excel

/**
 * @swagger
 * /api/admin/indexStatistics:
 *    get:
 *      tags:
 *      - 首页相关
 *      summary: 首页统计-查询
 *      produces:
 *      - application/json
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/indexStatistics", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code != 200) return;
    let sql = createSql.select("web_menu.title as `name` ,COUNT(web_menu.title) as `value`")
        .from("my_article,web_menu")
        .where("my_article.typeID = web_menu.ID")
        .group("web_menu.title").end();
    let data = await Db.query(sql, res);
    return res.json({
        ...successCode[200],
        data
    });
});


/**
 * @swagger
 * /api/admin/getPower:
 *    get:
 *      tags:
 *      - 权限相关
 *      summary: 用户权限和菜单-查询
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: power
 *          description: 权限ID
 *          in: query
 *          required: true
 *          type: string
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getPower", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code != 200) return;
    let { power } = req.query;
    if (!power) {
        res.status(500);
        return res.json({
            ...errorCode[10003]
        })
    }
    let sql = createSql.select().from("admin_user_roles").where(`ID = '${power}'`).end();
    let data = await Db.query(sql, res);
    if (data.length == 0) {
        res.status(500);
        return res.json({
            ...errorCode[10001],
            message: "该权限不存在,请联系管理员"
        })
    }
    data = data[0];
    let role = data.role.slice(0, data.role.length - 1);
    sql = createSql.select().from("admin_user_menu").where(`is_show = 1 AND is_del != 1 AND ID in (${role})`).order("sorts").end();
    let menu = await Db.query(sql, res);
    if (menu.length == 0) {
        res.status(500);
        return res.json({
            ...errorCode[10001],
            message: "该权限下没有菜单"
        })
    }
    let menuList = JSON.parse(JSON.stringify(menu));
    let pMenu = menuList.filter(item => {
        item.list = [];
        return !item.pid;
    });
    pMenu.forEach(item => {
        menuList.forEach(i => {
            if (item.ID == i.pid) {
                item.list.push(i);
            }
        })
    });
    res.json({
        ...successCode[200],
        data: {
            menu: pMenu,
            jurisdiction: data.jurisdiction
        }
    })
});

/**
 * @swagger
 * /api/admin/getShowUserInfo:
 *    get:
 *      tags:
 *      - 后台管理 个人信息
 *      summary: 个人展示信息-列表查询
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: pageSize
 *          default: 10
 *          description: 条数
 *          in: query
 *          required: false
 *          type: string
 *        - name: page
 *          default: 1
 *          description: 页码
 *          in: query
 *          required: false
 *          type: string
 *        - name: username
 *          description: 用户名
 *          in: query
 *          required: false
 *          type: string
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getShowUserInfo", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { pageSize, page, username } = req.query;
    pageSize = pageSize || 10;
    page = page || 10;
    let sql = createSql.select().from("my_userinfo").where("is_del != 1").order("addtime DESC").limit(`${pageSize * (page - 1)}, ${pageSize}`).end()
    let conutSql = createSql.select("count(*) as count").from("my_userinfo").where("is_del != 1").end();
    if (username) {
        sql = createSql.select().from("my_userinfo").where(`username LIKE '%${json.username}%' AND is_del != 1`).order("addtime DESC").limit(`${pageSize * (page - 1)}, ${pageSize}`).end();
        conutSql = createSql.select("count(*) as count").from("my_userinfo").where(`username LIKE '%${json.username}%' AND is_del != 1`).end();
    }
    let data = await Db.query(sql, res);
    if (data.code) return;
    let countData = await Db.query(conutSql);
    if (countData.code) return;
    data.forEach(v => {
        v.addtime = v.addtime ? utils.getTime(null, v.addtime) : "1997-10-15 10:00:00",
            v.usericon = v.usericon ? config.httpImgUrl + v.usericon : ""
    })
    return res.json({
        ...successCode[200],
        data: {
            data: data,
            count: countData[0].count
        }
    })
})

/**
 * @swagger
 * /api/admin/addShowUserInfo:
 *    post:
 *      tags:
 *      - 后台管理 个人信息
 *      summary: 个人展示信息-添加
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                username:
 *                  type: string
 *                  description: 用户名
 *                  required: true
 *                usericon:
 *                  type: string
 *                  description: 用户头像
 *                  required: true
 *                brief:
 *                  type: string
 *                  description: 简介
 *                work_years:
 *                  type: string
 *                  description: 工作年限
 *                tel:
 *                  type: string
 *                  description: 电话
 *                email:
 *                  type: string
 *                  description: 邮箱
 *                address:
 *                  type: string
 *                  description: 地址
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/addShowUserInfo", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let data = utils.checkData(req.body, objectCheck.userInfo_add);
    if (data.code > 200) return res.json(data);
    data = data.data;
    data.createByUser = isLogin.data.ID;
    data.addtime = utils.getTime();
    data.ID = utils.getUuid();
    let sql = "";
    if (data.is_show == 1) {
        sql = createSql.update("my_userinfo").set("is_show = 0").where("is_show = 1").end();
        await Db.query(sql, res);
    }
    sql = createSql.insert("my_userinfo").value(data).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
});

/**
 * @swagger
 * /api/admin/updateShowUserInfo:
 *    post:
 *      tags:
 *      - 后台管理 个人信息
 *      summary: 个人展示信息-修改
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: ID
 *                  required: true
 *                username:
 *                  type: string
 *                  description: 用户名 只有is_show这个参数的时候可以不传
 *                usericon:
 *                  type: string
 *                  description: 用户头像 只有is_show这个参数的时候可以不传
 *                brief:
 *                  type: string
 *                  description: 简介
 *                work_years:
 *                  type: string
 *                  description: 工作年限
 *                tel:
 *                  type: string
 *                  description: 电话
 *                email:
 *                  type: string
 *                  description: 邮箱
 *                address:
 *                  type: string
 *                  description: 地址
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *      responses:
 *        200:
 *          description: 返回结果
 * */

router.post("/updateShowUserInfo", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    const { ID, is_show, username } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    let data = {};
    let sql = "";
    if (username) {
        data = utils.checkData(req.body, objectCheck.userInfo_add);
        data.createByUser = isLogin.data.ID;
        data.updatetime = utils.getTime();
    }
    if (is_show == 1) {
        sql = createSql.update("my_userinfo").set("is_show = 0").where("is_show = 1").end();
        await Db.query(sql, res);
    }
    if (username) {
        sql = createSql.update("my_userinfo").set(data).where(`ID = '${ID}'`).end();
    } else {
        sql = createSql.update("my_userinfo").set({ is_show: is_show }).where(`ID = '${ID}'`).end();
    }
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
});


/**
 * @swagger
 * /api/admin/delShowUserInfo:
 *    post:
 *      tags:
 *      - 后台管理 个人信息
 *      summary: 个人展示信息-删除
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: 用户ID
 *      responses:
 *        200:
 *          description: 返回结果
 * */

router.post("/delShowUserInfo", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    const { ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    let sql = createSql.select("usericon").from("my_userinfo").where(`ID = '${ID}'`).end();
    let data = await Db.query(sql, res);
    if (data.code) return;
    if (!data.length) {
        return res.json({
            ...errorCode[10008],
            message: "没有找到该数据"
        })
    }
    data = data[0];
    // 删除文件
    let delFolder = path.resolve(__dirname, '../upload/' + data.usericon);
    utils.deleteFile(delFolder);
    data = {
        updatetime: utils.getTime(),
        createByUser: isLogin.data.ID,
        is_del: 1
    }
    sql = createSql.update("my_userinfo").set(data).where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/getBlogTiltleBrfie:
 *    get:
 *      tags:
 *      - 后台管理 博客标题和简介
 *      summary: 博客标题和简介-列表查询
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: pageSize
 *          default: 10
 *          description: 条数
 *          in: query
 *          required: false
 *          type: string
 *        - name: page
 *          default: 1
 *          description: 页码
 *          in: query
 *          required: false
 *          type: string
 *        - name: title
 *          description: 标题
 *          in: query
 *          required: false
 *          type: string
 *      responses:
 *        200:
 *         description: successful operation
 * */

router.get("/getBlogTiltleBrfie", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { pageSize, page, title } = req.query;
    pageSize = pageSize || 10;
    page = page || 1;
    let sql = createSql.select().from("blog_title_brfie").where(`is_del = 0`).order("addtime DESC").limit(`${pageSize * (page - 1)} , ${pageSize}`).end();
    let sqlCount = createSql.select("count(*) as count").from("blog_title_brfie").where(`is_del = 0`).end();
    if (title) {
        sql = createSql.select().from("blog_title_brfie").where(`is_del = 0 and title like '%${title}%'`).order("addtime DESC").limit(`${pageSize * (page - 1)} , ${pageSize}`).end();
        sqlCount = createSql.select("count(*) as count").from("blog_title_brfie").where(`is_del = 0 and title like '%${title}%'`).end();
    }
    let result = await Db.query(sql, res);
    if (result.code) return;
    let count = await Db.query(sqlCount, res);
    if (count.code) return;
    result.forEach(v => {
        v.addtime = v.addtime ? utils.getTime(null, v.addtime) : "1997-10-15 10:00:00",
            v.updatetime = v.updatetime ? utils.getTime(null, v.updatetime) : "1997-10-15 10:00:00"
    })

    return res.json({
        ...successCode[200],
        data: {
            data: result,
            count: count[0].count
        }
    })
})

/**
 * @swagger
 * /api/admin/addBlogTiltleBrfie:
 *    post:
 *      tags:
 *      - 后台管理 博客标题和简介
 *      summary: 博客标题和简介-添加
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                title:
 *                  type: string
 *                  description: 标题
 *                  required: true
 *                brfie:
 *                  type: string
 *                  description: 简介
 *                  required: true
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *      responses:
 *        200:
 *          description: 返回结果
 * */

router.post("/addBlogTiltleBrfie", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let data = utils.checkData(req.body, objectCheck.titleAndBrfie_add);
    if (data.code > 200) {
        return res.json(data)
    }
    data = data.data;
    data.createByUser = isLogin.data.ID;
    data.addtime = utils.getTime();
    data.ID = utils.getUuid();
    let sql = createSql.insert("blog_title_brfie").value(data).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/updataBlogTiltleBrfie:
 *    post:
 *      tags:
 *      - 后台管理 博客标题和简介
 *      summary: 博客标题和简介-修改
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: ID
 *                title:
 *                  type: string
 *                  description: 标题
 *                  required: true
 *                brfie:
 *                  type: string
 *                  description: 简介
 *                  required: true
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/updataBlogTiltleBrfie", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { title, ID, is_show } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    let data = {};
    if (title) {
        data = utils.checkData(req.body, objectCheck.titleAndBrfie_add);
        if (data.code > 200) {
            return res.json(data)
        }
        data = data.data;
    } else {
        data = { is_show: is_show }
    }
    data.updatetime = utils.getTime();
    data.createByUser = isLogin.data.ID;
    let sql = createSql.update("blog_title_brfie").set(data).where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/delBlogTiltleBrfie:
 *    post:
 *      tags:
 *      - 后台管理 博客标题和简介
 *      summary: 博客标题和简介-删除
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: 用户ID
 *      responses:
 *        200:
 *          description: 返回结果
 * */

router.post("/delBlogTiltleBrfie", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    const { ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    data = {
        updatetime: utils.getTime(),
        createByUser: isLogin.data.ID,
        is_del: 1
    }
    sql = createSql.update("blog_title_brfie").set(data).where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/getSlider:
 *    get:
 *      tags:
 *      - 后台管理 后台菜单
 *      summary: 后台菜单-列表查询
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: pageSize
 *          default: 10
 *          description: 条数
 *          in: query
 *          required: false
 *          type: string
 *        - name: page
 *          default: 1
 *          description: 页码
 *          in: query
 *          required: false
 *          type: string
 *        - name: title
 *          description: 标题
 *          in: query
 *          required: false
 *          type: string
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getSlider", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { pageSize, page, title } = req.query;
    pageSize = pageSize || 10;
    page = page || 1;
    let sql = "";
    let sqlCount = "";
    let allSql = createSql.select().from("admin_user_menu").where(`is_del = 0`).order("sorts ASC").end();
    if (title) {
        sql = createSql.select().from("admin_user_menu").where(`is_del = 0 AND title like '%${title}%'`).order("sorts ASC").limit(`${pageSize * (page - 1)} , ${pageSize}`).end();
        sqlCount = createSql.select("count(*) as count").from("admin_user_menu").where(`is_del = 0 AND title like '%${title}%'`).end();
    }
    if (!title) {
        sql = createSql.select().from("admin_user_menu").where(`(pid is null or pid = '') AND is_del = 0`).order("sorts ASC").limit(`${pageSize * (page - 1)} , ${pageSize}`).end();
        sqlCount = createSql.select("count(*) as count").from("admin_user_menu").where(`(pid is null or pid = '') AND is_del = 0`).end();
    }
    let result = await Db.query(sql, res);
    if (result.code) return;
    let count = await Db.query(sqlCount, res);
    if (count.code) return;
    let allData = await Db.query(allSql, res);
    if (allData.code) return;
    result.forEach(v => {
        v.addtime = v.addtime ? utils.getTime(null, v.addtime) : "1997-10-15 10:00:00",
            v.updatetime = v.updatetime ? utils.getTime(null, v.updatetime) : "1997-10-15 10:00:00"
    });
    allData.forEach(v => {
        v.addtime = v.addtime ? utils.getTime(null, v.addtime) : "1997-10-15 10:00:00",
            v.updatetime = v.updatetime ? utils.getTime(null, v.updatetime) : "1997-10-15 10:00:00"
    })
    result.forEach(v => {
        v.children = allData.filter(item => item.pid == v.ID);
    })
    return res.json({
        ...successCode[200],
        data: {
            data: result,
            count: count[0].count
        }
    })
});

/**
 * @swagger
 * /api/admin/getAllSlider:
 *    get:
 *      tags:
 *      - 后台管理 后台菜单
 *      summary: 后台菜单-查询所有
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: pageSize
 *          default: 10
 *          description: 条数
 *          in: query
 *          required: false
 *          type: string
 *        - name: page
 *          default: 1
 *          description: 页码
 *          in: query
 *          required: false
 *          type: string
 *        - name: all
 *          description: 是否查询所有
 *          in: query
 *          required: false
 *          type: string
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getAllSlider", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let sql = createSql.select().from("admin_user_menu").where(`is_del = 0`).order("addtime DESC").end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    result.forEach(v => {
        v.addtime = v.addtime ? utils.getTime(null, v.addtime) : "1997-10-15 10:00:00",
            v.updatetime = v.updatetime ? utils.getTime(null, v.updatetime) : "1997-10-15 10:00:00"
    })
    return res.json({
        ...successCode[200],
        data: result
    })
})

/**
 * @swagger
 * /api/admin/addSlider:
 *    post:
 *      tags:
 *      - 后台管理 后台菜单
 *      summary: 后台菜单-添加
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                title:
 *                  type: string
 *                  description: 标题
 *                  required: true
 *                pid:
 *                  type: string
 *                  description: 父级ID
 *                title_en:
 *                  type: string
 *                  description: 英文标题
 *                path:
 *                  type: string
 *                  description: 页面跳转地址
 *                pageUrl:
 *                  type: string
 *                  description: 页面文件地址
 *                icon_name:
 *                  type: string
 *                  description: 图标分类名称
 *                icon:
 *                  type: string
 *                  description: 图标名称
 *                sorts:
 *                  type: string
 *                  description: 排序
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/addSlider", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let data = utils.checkData(req.body, objectCheck.slider_add);
    if (data.code > 200) {
        return res.json(data)
    }
    data = data.data;
    data.createByUser = isLogin.data.ID;
    data.addtime = utils.getTime();
    data.ID = utils.getUuid();
    if (!data.pid) {
        data.pid = null;
    }
    if (!data.sorts) {
        data.sorts = 999;
    }
    let sql = createSql.insert("admin_user_menu").value(data).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/updataSlider:
 *    post:
 *      tags:
 *      - 后台管理 后台菜单
 *      summary: 后台菜单-更新
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: ID
 *                  required: true
 *                title:
 *                  type: string
 *                  description: 标题
 *                pid:
 *                  type: string
 *                  description: 父级ID
 *                title_en:
 *                  type: string
 *                  description: 英文标题
 *                path:
 *                  type: string
 *                  description: 页面跳转地址
 *                pageUrl:
 *                  type: string
 *                  description: 页面文件地址
 *                icon_name:
 *                  type: string
 *                  description: 图标分类名称
 *                icon:
 *                  type: string
 *                  description: 图标名称
 *                sorts:
 *                  type: string
 *                  description: 排序
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/updataSlider", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { title, ID, is_show } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    let data = {};
    if (title) {
        data = utils.checkData(req.body, objectCheck.slider_add);
        if (data.code > 200) {
            return res.json(data)
        }
        data = data.data;
    } else {
        data = { is_show: is_show }
    }
    data.updatetime = utils.getTime();
    let sql = createSql.update("admin_user_menu").set(data).where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/delSlider:
 *    post:
 *      tags:
 *      - 后台管理 后台菜单
 *      summary: 后台菜单-删除
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: 用户ID
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/delSlider", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    const { ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    data = {
        updatetime: utils.getTime(),
        createByUser: isLogin.data.ID,
        is_del: 1
    }
    sql = createSql.update("admin_user_menu").set(data).where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})


/**
 * @swagger
 * /api/admin/getLinks:
 *    get:
 *      tags:
 *      - 后台管理 友情链接
 *      summary: 友情链接-列表查询
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: pageSize
 *          default: 10
 *          description: 条数
 *          in: query
 *          required: false
 *          type: string
 *        - name: page
 *          default: 1
 *          description: 页码
 *          in: query
 *          required: false
 *          type: string
 *        - name: title
 *          description: 标题
 *          in: query
 *          required: false
 *          type: string
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getLinks", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { pageSize, page, title } = req.query;
    pageSize = pageSize || 10;
    page = page || 1;
    let sql = createSql.select().from("friendly_links").where(`is_del = 0`).order("addtime DESC").limit(`${pageSize * (page - 1)} , ${pageSize}`).end();
    let sqlCount = createSql.select("count(*) as count").from("friendly_links").where(`is_del = 0`).end();
    if (title) {
        sql = createSql.select().from("friendly_links").where(`is_del = 0 and title like '%${title}%'`).order("addtime DESC").limit(`${pageSize * (page - 1)} , ${pageSize}`).end();
        sqlCount = createSql.select("count(*) as count").from("friendly_links").where(`is_del = 0 and title like '%${title}%'`).end();
    }
    let result = await Db.query(sql, res);
    if (result.code) return;
    let count = await Db.query(sqlCount, res);
    if (count.code) return;
    result.forEach(v => {
        v.addtime = v.addtime ? utils.getTime(null, v.addtime) : "1997-10-15 10:00:00",
            v.updatetime = v.updatetime ? utils.getTime(null, v.updatetime) : "1997-10-15 10:00:00"
    })

    return res.json({
        ...successCode[200],
        data: {
            data: result,
            count: count[0].count
        }
    })
})

/**
 * @swagger
 * /api/admin/addLinks:
 *    post:
 *      tags:
 *      - 后台管理 友情链接
 *      summary: 友情链接-添加
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                title:
 *                  type: string
 *                  description: 标题
 *                  required: true
 *                url:
 *                  type: string
 *                  description: 链接地址
 *                  required: true
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *      responses:
 *        200:
 *          description: 返回结果
 * */

router.post("/addLinks", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let data = utils.checkData(req.body, objectCheck.links_add);
    if (data.code > 200) {
        return res.json(data)
    }
    data = data.data;
    data.createByUser = isLogin.data.ID;
    data.addtime = utils.getTime();
    data.ID = utils.getUuid();
    let sql = createSql.insert("friendly_links").value(data).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/updataLinks:
 *    post:
 *      tags:
 *      - 后台管理 友情链接
 *      summary: 友情链接-修改
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: ID
 *                title:
 *                  type: string
 *                  description: 标题
 *                  required: true
 *                url:
 *                  type: string
 *                  description: 链接地址
 *                  required: true
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/updataLinks", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { title, ID, is_show } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    let data = {};
    if (title) {
        data = utils.checkData(req.body, objectCheck.links_add);
        if (data.code > 200) {
            return res.json(data)
        }
        data = data.data;
    } else {
        data = { is_show: is_show }
    }
    data.updatetime = utils.getTime();
    data.createByUser = isLogin.data.ID;
    let sql = createSql.update("friendly_links").set(data).where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/delLinks:
 *    post:
 *      tags:
 *      - 后台管理 友情链接
 *      summary: 友情链接-删除
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: 用户ID
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/delLinks", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    const { ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    data = {
        updatetime: utils.getTime(),
        createByUser: isLogin.data.ID,
        is_del: 1
    }
    sql = createSql.update("friendly_links").set(data).where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/getSearch:
 *    get:
 *      tags:
 *      - 后台管理 搜索管理
 *      summary: 搜索管理-列表查询
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: pageSize
 *          default: 10
 *          description: 条数
 *          in: query
 *          required: false
 *          type: string
 *        - name: page
 *          default: 1
 *          description: 页码
 *          in: query
 *          required: false
 *          type: string
 *        - name: title
 *          description: 标题
 *          in: query
 *          required: false
 *          type: string
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getSearch", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { pageSize, page, title } = req.query;
    pageSize = pageSize || 10;
    page = page || 1;
    let sql = createSql.select().from("search_settings").where(`is_del = 0`).order("addtime DESC").limit(`${pageSize * (page - 1)} , ${pageSize}`).end();
    let sqlCount = createSql.select("count(*) as count").from("search_settings").where(`is_del = 0`).end();
    if (title) {
        sql = createSql.select().from("search_settings").where(`is_del = 0 and title like '%${title}%'`).order("addtime DESC").limit(`${pageSize * (page - 1)} , ${pageSize}`).end();
        sqlCount = createSql.select("count(*) as count").from("search_settings").where(`is_del = 0 and title like '%${title}%'`).end();
    }
    let result = await Db.query(sql, res);
    if (result.code) return;
    let count = await Db.query(sqlCount, res);
    if (count.code) return;
    result.forEach(v => {
        v.addtime = v.addtime ? utils.getTime(null, v.addtime) : "1997-10-15 10:00:00",
            v.updatetime = v.updatetime ? utils.getTime(null, v.updatetime) : "1997-10-15 10:00:00"
    })

    return res.json({
        ...successCode[200],
        data: {
            data: result,
            count: count[0].count
        }
    })
})

/**
 * @swagger
 * /api/admin/addSearch:
 *    post:
 *      tags:
 *      - 后台管理 搜索管理
 *      summary: 搜索管理-添加
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                title:
 *                  type: string
 *                  description: 标题
 *                  required: true
 *                url:
 *                  type: string
 *                  description: 链接地址
 *                  required: true
 *                is_stop:
 *                  type: number
 *                  description: 是否停用
 *                is_activity:
 *                  type: number
 *                  description: 是否活动
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/addSearch", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let data = utils.checkData(req.body, objectCheck.search_add);
    if (data.code > 200) {
        return res.json(data)
    }
    data = data.data;
    let sql = createSql.select().from("search_settings").where(`title = '${data.title}' AND is_del != 1`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    if (result.length) {
        return res.json({
            ...errorCode[10002],
            message: '关键词已存在'
        })
    }
    data.ID = utils.getUuid();
    data.addtime = utils.getTime();
    data.createByUser = isLogin.data.ID;
    sql = createSql.insert("search_settings").value(data).end();
    result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/updataSearch:
 *    post:
 *      tags:
 *      - 后台管理 搜索管理
 *      summary: 搜索管理-修改
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: ID
 *                title:
 *                  type: string
 *                  description: 标题
 *                  required: true
 *                url:
 *                  type: string
 *                  description: 链接地址
 *                  required: true
 *                is_stop:
 *                  type: number
 *                  description: 是否停用
 *                is_activity:
 *                  type: number
 *                  description: 是否活动
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/updataSearch", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { title, ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }

    let data = {};
    if (title) {
        data = utils.checkData(req.body, objectCheck.links_add);
        if (data.code > 200) {
            return res.json(data)
        }
        data = data.data;
    } else {
        data = req.body;
    }
    let sql = createSql.select().from("search_settings").where(`title = '${data.title}' AND is_del != 1 AND ID != '${data.ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    if (result.length) {
        return res.json({
            ...errorCode[10002],
            message: '关键词已存在'
        })
    }
    delete data.ID;
    data.updatetime = utils.getTime();
    data.createByUser = isLogin.data.ID;
    sql = createSql.update("search_settings").set(data).where(`ID = '${ID}'`).end();
    result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/delSearch:
 *    post:
 *      tags:
 *      - 后台管理 搜索管理
 *      summary: 搜索管理-删除
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: 用户ID
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/delSearch", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    let sql = createSql.update("search_settings").set({ is_del: 1, updatetime: utils.getTime() }).where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/getCommit:
 *    get:
 *      tags:
 *      - 后台管理 留言数据
 *      summary: 留言数据-列表查询
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: pageSize
 *          default: 10
 *          description: 条数
 *          in: query
 *          required: false
 *          type: string
 *        - name: page
 *          default: 1
 *          description: 页码
 *          in: query
 *          required: false
 *          type: string
 *        - name: content
 *          description: 内容
 *          in: query
 *          required: false
 *          type: string
 *        - name: article
 *          description: 文章名称
 *          in: query
 *          required: false
 *          type: string
 *        - name: username
 *          description: 用户名
 *          in: query
 *          required: false
 *          type: string
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getCommit", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { pageSize, page, content, article, username } = req.query;
    pageSize = pageSize || 10;
    page = page || 1;
    let isAdd = false;
    let where = ""
    if (content) {
        where += `content LIKE '%${content}%'`
        isAdd = true
    }
    if (article) {
        where += isAdd ? ` AND article_title LIKE '%${article}%'` : ` article_title LIKE '%${article}%'`;
        isAdd = true;
    }
    if (username) {
        where += isAdd ? ` AND username LIKE '%${username}%'` : ` username LIKE '%${username}%'`;
        isAdd = true;
    }
    let countWhere = isAdd ? where + ` AND is_del != 1` : ` reply_id = '' AND is_del != 1`;
    where += isAdd ? ` AND is_del != 1` : ` is_del != 1`;
    let sql = createSql.select().from("article_comment_view").where(where).limit(`${pageSize * (page - 1)}, ${pageSize}`).end();
    let countSql = createSql.select("count(*) as count").from("article_comment_view").where(countWhere).end();
    let result = await Db.query(sql, res);
    let countResult = await Db.query(countSql, res);
    if (result.code) return;
    if (countResult.code) return;
    result.forEach(v => {
        v.addtime = v.addtime ? utils.getTime(null, v.addtime) : "1997-10-15 10:00:00";
        v.updatetime = v.updatetime ? utils.getTime(null, v.updatetime) : "1997-10-15 10:00:00";
        v.children = [];
    })
    let parent = result.filter(v => v.reply_id === "");
    parent.forEach(v => {
        v.children = result.filter(item => item.reply_id === v.ID);
    })
    return res.json({
        ...successCode[200],
        data: {
            data: isAdd ? result : parent,
            count: countResult[0].count
        }
    })
})

/**
 * @swagger
 * /api/admin/updateCommit:
 *    post:
 *      tags:
 *      - 后台管理 留言数据
 *      summary: 留言数据-加精 顶置 是否显示
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: ID
 *                is_refining:
 *                  type: string
 *                  description: 加精
 *                is_top:
 *                  type: string
 *                  description: 顶置
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/updateCommit", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { is_refining, is_top, is_show, ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    let data = {};
    if (is_refining != undefined) {
        data.is_refining = is_refining;
    }
    if (is_top != undefined) {
        data.is_top = is_top;
    }
    if (is_show != undefined) {
        data.is_show = is_show;
    }
    data.updatetime = utils.getTime();
    let sql = createSql.update("article_comment").set(data).where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/delCommit:
 *    post:
 *      tags:
 *      - 后台管理 留言数据
 *      summary: 留言数据-删除
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: 用户ID
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/delCommit", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    let sql = createSql.update("article_comment").set({ is_del: 1, updatetime: utils.getTime() }).where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/getLable:
 *    get:
 *      tags:
 *      - 后台管理 我的标签
 *      summary: 我的标签-列表查询
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: pageSize
 *          default: 10
 *          description: 条数
 *          in: query
 *          required: false
 *          type: string
 *        - name: page
 *          default: 1
 *          description: 页码
 *          in: query
 *          required: false
 *          type: string
 *        - name: title
 *          description: 标签内容
 *          in: query
 *          required: false
 *          type: string
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getLable", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { pageSize, page, title } = req.query;
    pageSize = pageSize || 10;
    page = page || 1;
    let isAdd = false;
    let where = "";
    if (title) {
        where += ` title like '%${title}%'`;
        isAdd = true;
    }
    let countWhere = isAdd ? where + ` AND is_del != 1` : ` is_del != 1`;
    where += isAdd ? ` AND is_del != 1` : ` is_del != 1`;
    let sql = createSql.select().from("my_label").where(where).limit(`${pageSize * (page - 1)}, ${pageSize}`).end();
    let countSql = createSql.select("count(*) as count").from("my_label").where(countWhere).end();
    let result = await Db.query(sql, res);
    let countResult = await Db.query(countSql, res);
    if (result.code) return;
    if (countResult.code) return;
    result.forEach(v => {
        v.addtime = v.addtime ? utils.getTime(null, v.addtime) : "1997-10-15 10:00:00";
        v.updatetime = v.updatetime ? utils.getTime(null, v.updatetime) : "1997-10-15 10:00:00";
    })
    return res.json({
        ...successCode[200],
        data: {
            data: result,
            count: countResult[0].count
        }
    })

})

/**
 * @swagger
 * /api/admin/addLable:
 *    post:
 *      tags:
 *      - 后台管理 我的标签
 *      summary: 我的标签-添加
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                title:
 *                  type: string
 *                  description: 标题
 *                  required: true
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/addLable", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let data = utils.checkData(req.body, objectCheck.label_add);
    if (data.code > 200) {
        return res.json(data)
    }
    data = data.data;
    let sql = createSql.select().from("my_label").where(`title = '${data.title}' AND is_del != 1`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    if (result.length) {
        return res.json({
            ...errorCode[10002],
            message: '关键词已存在'
        })
    }
    let red = Math.floor(Math.random() * (255 - 0));
    let green = Math.floor(Math.random() * (255 - 0));
    let blue = Math.floor(Math.random() * (255 - 0));
    let color = `${red}, ${green}, ${blue}, 0.5`;
    data.color = color;
    data.ID = utils.getUuid();
    data.addtime = utils.getTime();
    data.createByUser = isLogin.data.ID;
    sql = createSql.insert("my_label").value(data).end();
    result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/delLable:
 *    post:
 *      tags:
 *      - 后台管理 我的标签
 *      summary: 我的标签-删除
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: 用户ID
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/delLable", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    let sql = createSql.update("my_label").set({ is_del: 1, updatetime: utils.getTime() }).where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/getBanner:
 *    get:
 *      tags:
 *      - 后台管理 轮播图管理
 *      summary: 轮播图管理-列表查询
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: pageSize
 *          default: 10
 *          description: 条数
 *          in: query
 *          required: false
 *          type: string
 *        - name: page
 *          default: 1
 *          description: 页码
 *          in: query
 *          required: false
 *          type: string
 *        - name: title
 *          description: 轮播图名称
 *          in: query
 *          required: false
 *          type: string
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getBanner", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { pageSize, page, title } = req.query;
    pageSize = pageSize || 10;
    page = page || 1;
    let isAdd = false;
    let where = "";
    if (title) {
        where += ` title like '%${title}%'`;
        isAdd = true;
    }
    let countWhere = isAdd ? where + ` AND is_del != 1` : ` is_del != 1`;
    where += isAdd ? ` AND is_del != 1` : ` is_del != 1`;
    let sql = createSql.select().from("banner").where(where).order('addtime DESC').limit(`${pageSize * (page - 1)}, ${pageSize}`).end();
    let countSql = createSql.select("count(*) as count").from("banner").where(countWhere).end();
    let result = await Db.query(sql, res);
    let countResult = await Db.query(countSql, res);
    if (result.code) return;
    if (countResult.code) return;
    result.forEach(v => {
        v.addtime = v.addtime ? utils.getTime(null, v.addtime) : "1997-10-15 10:00:00";
        v.updatetime = v.updatetime ? utils.getTime(null, v.updatetime) : "1997-10-15 10:00:00";
        v.path = v.path ? decodeURIComponent(v.path) : '';
        v.mpPath = v.mpPath ? decodeURIComponent(v.mpPath) : '';
        if (v.ImgUrl) {
            v.ImgUrl = global.ip + v.ImgUrl
        }
    })
    return res.json({
        ...successCode[200],
        data: {
            data: result,
            count: countResult[0].count
        }
    })
})

/**
 * @swagger
 * /api/admin/addBanner:
 *    post:
 *      tags:
 *      - 后台管理 轮播图管理
 *      summary: 轮播图管理-添加
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                title:
 *                  type: string
 *                  description: 标题
 *                  required: true
 *                ImgUrl:
 *                  type: string
 *                  description: 图片地址
 *                mpPath:
 *                  type: number
 *                  description: 小程序地址
 *                path:
 *                  type: number
 *                  description: 跳转地址
 *                sorts:
 *                  type: number
 *                  description: 排序
 *                is_text:
 *                  type: number
 *                  description: 是否显示文字
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *      responses:
 *        200:
 *          description: 返回结果
 * */

router.post("/addBanner", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let data = utils.checkData(req.body, objectCheck.banner_add);
    if (data.code > 200) return res.json(data);
    data = data.data;
    data.createByUser = isLogin.data.ID;
    data.addtime = utils.getTime();
    data.ID = utils.getUuid();
    let sql = "";
    if (data.mpPath) {
        data.mpPath = encodeURIComponent(data.mpPath);
    }
    if (data.path) {
        data.path = encodeURIComponent(data.path);
    }
    sql = createSql.insert("banner").value(data).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/updataBanner:
 *    post:
 *      tags:
 *      - 后台管理 轮播图管理
 *      summary: 轮播图管理-更新
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: 标题
 *                  required: true
 *                title:
 *                  type: string
 *                  description: 标题
 *                ImgUrl:
 *                  type: string
 *                  description: 图片地址
 *                mpPath:
 *                  type: number
 *                  description: 小程序地址
 *                path:
 *                  type: number
 *                  description: 跳转地址
 *                sorts:
 *                  type: number
 *                  description: 排序
 *                is_text:
 *                  type: number
 *                  description: 是否显示文字
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/updataBanner", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { title, ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }

    let data = {};
    if (title) {
        data = utils.checkData(req.body, objectCheck.banner_add);
        if (data.code > 200) {
            return res.json(data)
        }
        data = data.data;
    } else {
        data = req.body;
    }
    let sql = "";
    if (data.ImgUrl) {
        sql = createSql.select().from("banner").where(`ID = '${ID}'`).end();
        let select = await Db.query(sql, res);
        if (data.ImgUrl != select[0].ImgUrl) {
            if (select.code < 0) return;
            if (select[0].ImgUrl) {
                let delFile = path.resolve(__dirname, '../upload/' + select[0].ImgUrl);
                utils.deleteFile({
                    filePath: delFile
                })
            }
        }
    }
    delete data.ID;
    data.updatetime = utils.getTime();
    data.createByUser = isLogin.data.ID;
    console.log(data, 'data')
    if (data.mpPath) data.mpPath = encodeURIComponent(data.mpPath);
    sql = createSql.update("banner").set(data).where(`ID = '${ID}'`).end();
    result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/delBanner:
 *    post:
 *      tags:
 *      - 后台管理 轮播图管理
 *      summary: 轮播图管理-删除
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: 用户ID
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/delBanner", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    let sql = createSql.select().from("banner").where(`ID = '${ID}'`).end();
    let select = await Db.query(sql, res);
    if (select.code < 0) return;
    if (select[0].ImgUrl) {
        let delFile = path.resolve(__dirname, '../upload/' + select[0].ImgUrl);
        utils.deleteFile({
            filePath: delFile
        })
    }
    sql = createSql.update("banner").set({ is_del: 1, updatetime: utils.getTime() }).where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/getNav:
 *    get:
 *      tags:
 *      - 后台管理 web端菜单管理
 *      summary: web端菜单管理-列表查询
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: pageSize
 *          default: 10
 *          description: 条数
 *          in: query
 *          required: false
 *          type: string
 *        - name: page
 *          default: 1
 *          description: 页码
 *          in: query
 *          required: false
 *          type: string
 *        - name: title
 *          description: 轮播图名称
 *          in: query
 *          required: false
 *          type: string
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getNav", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { pageSize, page, title } = req.query;
    pageSize = pageSize || 10;
    page = page || 1;
    let isAdd = false;
    let where = "";
    if (title) {
        where += ` title like '%${title}%'`;
        isAdd = true;
    }
    let countWhere = isAdd ? where + ` AND is_del != 1 ` : ` is_del != 1`;
    where += isAdd ? ` AND is_del != 1'` : ` is_del != 1`;
    where += !title ? ` AND pid != ''` : "";
    countWhere += !title ? ` AND pid = ''` : "";
    let allWhere = isAdd ? where + ` AND is_del != 1 AND pid = ''` : ` is_del != 1 AND pid = ''`;
    let sql = createSql.select().from("web_menu").where(where).end();
    let countSql = createSql.select("count(*) as count").from("web_menu").where(!title ? countWhere : allWhere).end();
    let allSql = createSql.select().from("web_menu").where(allWhere).order("sorts").limit(`${pageSize * (page - 1)}, ${pageSize}`).end();
    let result = await Db.query(sql, res);
    let countResult = await Db.query(countSql, res);
    let allResult = await Db.query(allSql, res);
    if (result.code) return;
    if (countResult.code) return;
    if (allResult.code) return;
    result.forEach(v => {
        v.addtime = v.addtime ? utils.getTime(null, v.addtime) : "1997-10-15 10:00:00";
        v.updatetime = v.updatetime ? utils.getTime(null, v.updatetime) : "1997-10-15 10:00:00";
        v.icon = v.icon ? global.ip + v.icon : "";
    })
    allResult.forEach(v => {
        v.addtime = v.addtime ? utils.getTime(null, v.addtime) : "1997-10-15 10:00:00";
        v.updatetime = v.updatetime ? utils.getTime(null, v.updatetime) : "1997-10-15 10:00:00";
        v.icon = v.icon ? global.ip + v.icon : "";
        v.children = [];
    })
    allResult.forEach(v => {
        v.children = result.filter(item => item.pid === v.ID);
    })
    return res.json({
        ...successCode[200],
        data: {
            data: isAdd ? result : allResult,
            count: countResult[0].count
        }
    })
})

/**
 * @swagger
 * /api/admin/getNavForAdd:
 *    get:
 *      tags:
 *      - 后台管理 web端菜单管理
 *      summary: web端菜单管理-可以添加二级菜单的列表查询
 *      produces:
 *      - application/json
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getNavForAdd", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let sql = createSql.select().from("web_menu").where(`is_del != 1 AND pid = ''`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200],
        data: result
    })
})

/**
 * @swagger
 * /api/admin/addNav:
 *    post:
 *      tags:
 *      - 后台管理 web端菜单管理
 *      summary:  web端菜单管理-添加
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                title:
 *                  type: string
 *                  description: 标题
 *                  required: true
 *                pid:
 *                  type: string
 *                  description: 父级ID
 *                icon:
 *                  type: number
 *                  description: 图标
 *                path:
 *                  type: number
 *                  description: 跳转地址
 *                sorts:
 *                  type: number
 *                  description: 排序
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/addNav", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let data = utils.checkData(req.body, objectCheck.userInfo_add);
    if (data.code > 200) return res.json(data);
    data = data.data;
    data.createByUser = isLogin.data.ID;
    data.addtime = utils.getTime();
    data.ID = utils.getUuid();
    let sql = "";
    sql = createSql.insert("web_menu").value(data).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/updataNav:
 *    post:
 *      tags:
 *      - 后台管理 web端菜单管理
 *      summary:  web端菜单管理-更新
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                title:
 *                  type: string
 *                  description: 标题
 *                  required: true
 *                pid:
 *                  type: string
 *                  description: 父级ID
 *                icon:
 *                  type: number
 *                  description: 图标
 *                path:
 *                  type: number
 *                  description: 跳转地址
 *                sorts:
 *                  type: number
 *                  description: 排序
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/updataNav", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { title, ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }

    let data = {};
    if (title) {
        data = utils.checkData(req.body, objectCheck.nav_add);
        if (data.code > 200) {
            return res.json(data)
        }
        data = data.data;
    } else {
        data = req.body;
    }
    let sql = "";
    if (data.icon) {
        let imgUrlArr = data.icon.split("/");
        let pathArr = imgUrlArr.filter(item => { return item == "https:" || item == "http:" });
        if (pathArr.length) {
            delete data.icon
        }
        if (data.icon) {
            sql = createSql.select().from("web_menu").where(`ID = '${data.ID}'`).end();
            let select = await Db.query(sql, res);
            if (data.icon != select[0].icon) {
                if (select.code < 0) return;
                if (select[0].icon) {
                    let delFile = path.resolve(__dirname, '../upload/' + select[0].icon);
                    utils.deleteFile({
                        filePath: delFile
                    })
                }
            }
        }
    }
    delete data.ID;
    data.updatetime = utils.getTime();
    data.createByUser = isLogin.data.ID;
    sql = createSql.update("web_menu").set(data).where(`ID = '${ID}'`).end();
    result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/delNav:
 *    post:
 *      tags:
 *      - 后台管理 web端菜单管理
 *      summary: web端菜单管理-删除
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: 用户ID
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/delNav", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    let sql = createSql.select().from("web_menu").where(`ID = '${data.ID}'`).end();
    let select = await Db.query(sql, res);
    if (select.code < 0) return;
    if (select[0].icon) {
        let delFile = path.resolve(__dirname, '../upload/' + select[0].icon);
        utils.deleteFile({
            filePath: delFile
        })
    }
    sql = createSql.update("web_menu").set({ is_del: 1, updatetime: utils.getTime() }).where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/getAllNav:
 *    get:
 *      tags:
 *      - 后台管理 web端菜单管理
 *      summary: web端菜单管理-可以添加文章的列表查询
 *      produces:
 *      - application/json
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getAllNav", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let sql = createSql.select().from("web_menu").where(`is_del != 1`).and('is_article != 1').and("is_show = 1").end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200],
        data: result
    })
})

/**
 * @swagger
 * /api/admin/getArticle:
 *    get:
 *      tags:
 *      - 后台管理 文章管理
 *      summary: 文章管理-列表查询
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: pageSize
 *          default: 10
 *          description: 条数
 *          in: query
 *          required: false
 *          type: string
 *        - name: page
 *          default: 1
 *          description: 页码
 *          in: query
 *          required: false
 *          type: string
 *        - name: title
 *          description: 文章标题
 *          in: query
 *          required: false
 *          type: string
 *        - name: content
 *          description: 文章内容
 *          in: query
 *          required: false
 *          type: string
 *        - name: type
 *          description: 文章类型 ID
 *          in: query
 *          required: false
 *          type: string
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getArticle", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { pageSize, page, title, content, type } = req.query;
    pageSize = pageSize || 10;
    page = page || 1;
    let isAdd = false;
    let where = "";
    if (title) {
        where = ` title LIKE '%${title}%' `
        isAdd = true;
    }
    if (type) {
        where += isAdd ? ` AND typeID = '${type}' ` : ` typeID = '${type}' `;
        isAdd = true;
    }
    where += isAdd ? ` AND is_del != 1 ` : ` is_del != 1 `;
    let sql = createSql.select().from("article_view").where(where).order("addtime DESC").limit(`${pageSize * (page - 1)}, ${pageSize}`).end();
    let conutSql = createSql.select('count(*) as count').from("article_view").where(where).end();
    let result = await Db.query(sql, res);
    let count = await Db.query(conutSql, res);
    if (result.code || count.code) return;
    result.forEach(item => {
        item.addtime = item.addtime ? utils.getTime(item.addtime) : null;
        item.updatetime = item.updatetime ? utils.getTime(item.updatetime) : null;
        item.imgUrl = item.imgUrl ? global.ip + item.imgUrl : null;
        item.content = item.content ? decodeURIComponent(item.content) : null;
    })
    return res.json({
        ...successCode[200],
        data: {
            data: result,
            count: count[0].count
        },
    })
})

/**
 * @swagger
 * /api/admin/addArticle:
 *    post:
 *      tags:
 *      - 后台管理 文章管理
 *      summary:  文章管理-添加
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                title:
 *                  type: string
 *                  description: 标题
 *                  required: true
 *                reading:
 *                  type: string
 *                  description: 阅读量
 *                  required: true
 *                typeID:
 *                  type: string
 *                  description: 类型ID
 *                  required: true
 *                imgUrl:
 *                  type: string
 *                  description: 图片地址
 *                brfie:
 *                  type: string
 *                  description: 简介
 *                content:
 *                  type: string
 *                  description: 内容
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *                
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/addArticle", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let data = utils.checkData(req.body, objectCheck.article_add);
    if (data.code > 200) return res.json(data);
    data = data.data;
    data.createByUser = isLogin.data.ID;
    data.addtime = utils.getTime();
    data.ID = utils.getUuid();
    data.content = encodeURIComponent(data.content);
    let sql = "";
    sql = createSql.insert("my_article").value(data).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/updataArticle:
 *    post:
 *      tags:
 *      - 后台管理 文章管理
 *      summary:  文章管理-更新
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: 文章ID
 *                  required: true
 *                title:
 *                  type: string
 *                  description: 标题
 *                reading:
 *                  type: string
 *                  description: 阅读量
 *                typeID:
 *                  type: string
 *                  description: 类型ID
 *                imgUrl:
 *                  type: string
 *                  description: 图片地址
 *                brfie:
 *                  type: string
 *                  description: 简介
 *                content:
 *                  type: string
 *                  description: 内容
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *                
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/updataArticle", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { title, ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }

    let data = {};
    if (title) {
        data = utils.checkData(req.body, objectCheck.article_add);
        if (data.code > 200) {
            return res.json(data)
        }
        data = data.data;
    } else {
        data = req.body;
    }
    let sql = "";
    if (data.imgUrl) {
        let imgUrlArr = data.imgUrl.split("/");
        let pathArr = imgUrlArr.filter(item => { return item == "https:" || item == "http:" });
        let imgUrl = data.imgUrl;
        if (!pathArr.length) {
            imgUrl = "";
        }
        if (imgUrl) {
            sql = createSql.select().from("my_article").where(`ID = '${ID}'`).end();
            let select = await Db.query(sql, res);
            if (select.length && data.imgUrl != select[0].imgUrl) {
                if (select.code < 0) return;
                if (select[0].imgUrl) {
                    let delFile = path.resolve(__dirname, '../upload/' + select[0].imgUrl);
                    utils.deleteFile({
                        filePath: delFile
                    })
                }
            }
        }
    }
    delete data.ID;
    data.updatetime = utils.getTime();
    data.createByUser = isLogin.data.ID;
    data.content = data.content ? encodeURIComponent(data.content) : "";
    if (!data.content) delete data.content;
    sql = createSql.update("my_article").set(data).where(`ID = '${ID}'`).end();
    result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/delArticle:
 *    post:
 *      tags:
 *      - 后台管理 文章管理
 *      summary: 文章管理-删除
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: 文章ID
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/delArticle", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    let sql = createSql.select().from("my_article").where(`ID = '${data.ID}'`).end();
    let select = await Db.query(sql, res);
    if (select.code < 0) return;
    if (select[0].imgUrl) {
        let delFile = path.resolve(__dirname, '../upload/' + select[0].imgUrl);
        utils.deleteFile({
            filePath: delFile
        })
    }
    sql = createSql.update("my_article").set({ is_del: 1, updatetime: utils.getTime() }).where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/addSelfEvaluation:
 *    post:
 *      tags:
 *      - 后台管理 自我评价
 *      summary:  自我评价-添加
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                content:
 *                  type: string
 *                  description: 内容
 *                  required: true
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/addSelfEvaluation", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let data = utils.checkData(req.body, objectCheck.selfEvaluation_add);
    if (data.code > 200) return res.json(data);
    data = data.data;
    data.ID = utils.getUuid();
    data.addtime = utils.getTime();
    let sql = "";
    if (data.is_show) {
        sql = createSql.update("my_evaluation").set({ is_show: 0 }).where(`is_show = 1`).end();
        let result = await Db.query(sql, res);
        if (result.code) return;
    }
    sql = createSql.insert("my_evaluation").value(data).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200],
        data: {
            ID: data.ID
        }
    })
})

/**
 * @swagger
 * /api/admin/getSelfEvaluation:
 *    get:
 *      tags:
 *      - 后台管理 自我评价
 *      summary: 自我评价-列表查询
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: pageSize
 *          default: 10
 *          description: 条数
 *          in: query
 *          required: false
 *          type: string
 *        - name: page
 *          default: 1
 *          description: 页码
 *          in: query
 *          required: false
 *          type: string
 *        - name: content
 *          description: 文章内容
 *          in: query
 *          required: false
 *          type: string
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getSelfEvaluation", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { pageSize, page, content } = req.query;
    pageSize = pageSize || 10;
    page = page || 1;
    let where = "";
    if (content) {
        where = ` content LIKE '%${content}%' `
    }
    where += where ? ` AND is_del != 1 ` : ` is_del != 1 `;
    let sql = createSql.select().from("my_evaluation").where(where).order("addtime DESC").limit(`${pageSize * (page - 1)}, ${pageSize}`).end();
    let conutSql = createSql.select('count(*) as count').from("my_evaluation").where(where).end();
    let result = await Db.query(sql, res);
    let count = await Db.query(conutSql, res);
    if (result.code || count.code) return;

    result.forEach(item => {
        item.addtime = item.addtime ? utils.getTime(item.addtime) : null;
        item.updatetime = item.updatetime ? utils.getTime(item.updatetime) : null;
    })
    return res.json({
        ...successCode[200],
        data: {
            data: result,
            count: count[0].count
        },
    })
})

/**
 * @swagger
 * /api/admin/updateSelfEvaluation:
 *    post:
 *      tags:
 *      - 后台管理 自我评价
 *      summary:  自我评价-更新
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: ID
 *                  required: true
 *                content:
 *                  type: string
 *                  description: 内容
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/updateSelfEvaluation", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let data = {};
    if (!req.boy.content) {
        data = utils.checkData(req.body, objectCheck.selfEvaluation_add);
        if (data.code > 200) return res.json(data);
        data = data.data;
    } else {
        data = req.body;
    }
    if (!data.ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    let sql = "";
    if (data.is_show) {
        sql = createSql.update("my_evaluation").set({ is_show: 0 }).where(`is_show = 1`).end();
        let result = await Db.query(sql, res);
        if (result.code) return;
    }
    data.updatetime = utils.getTime();
    sql = createSql.update("my_evaluation").set(data).where(`ID = '${data.ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/delSelfEvaluation:
 *    post:
 *      tags:
 *      - 后台管理 自我评价
 *      summary: 自我评价-删除
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: 文章ID
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/delSelfEvaluation", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    let sql = createSql.select().from("my_evaluation").where(`ID = '${data.ID}'`).end();
    let select = await Db.query(sql, res);
    if (select.code < 0) return;
    sql = createSql.update("my_evaluation").set({ is_del: 1, updatetime: utils.getTime() }).where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/addAbility:
 *    post:
 *      tags:
 *      - 后台管理 我的能力
 *      summary:  我的能力-添加
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ability:
 *                  type: string
 *                  description: 能力
 *                  required: true
 *                sort:
 *                  type: Number
 *                  description: 排序
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *      responses:
 *        200:
 *          description: 返回结果
 * */

router.post("/addAbility", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let data = utils.checkData(req.body, objectCheck.Ability_add);
    if (data.code > 200) return res.json(data);
    data = data.data;
    if (!data.sorts) {
        data.sorts = 1;
    }
    data.ID = utils.getUuid();
    data.addtime = utils.getTime();
    let sql = createSql.insert("my_abilitys").value(data).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200],
        data: {
            ID: data.ID
        }
    })
})

/**
 * @swagger
 * /api/admin/updateAbility:
 *    post:
 *      tags:
 *      - 后台管理 我的能力
 *      summary:  我的能力-更新
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: ID
 *                  required: true
 *                ability:
 *                  type: string
 *                  description: 能力
 *                sort:
 *                  type: Number
 *                  description: 排序
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/updateAbility", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    let { ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    if (isLogin.code > 200) return;
    let data = {};
    if (!req.body.ability) {
        data = utils.checkData(req.body, objectCheck.Ability_add);
        if (data.code > 200) return res.json(data);
        data = data.data;
    } else {
        data = req.body;
    }

    let sql = "";
    sql = createSql.update("my_abilitys").set(data).where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/getAbility:
 *    get:
 *      tags:
 *      - 后台管理 我的能力
 *      summary: 我的能力-列表查询
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: pageSize
 *          default: 10
 *          description: 条数
 *          in: query
 *          required: false
 *          type: string
 *        - name: page
 *          default: 1
 *          description: 页码
 *          in: query
 *          required: false
 *          type: string
 *        - name: ability
 *          description: 能力
 *          in: query
 *          required: false
 *          type: string
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getAbility", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { pageSize, page, ability } = req.query;
    pageSize = pageSize || 10;
    page = page || 1;
    let where = "";
    if (ability) {
        where = ` ability LIKE '%${ability}%' `
    }
    where += where ? ` AND is_del != 1 ` : ` is_del != 1 `;
    let sql = createSql.select().from("my_abilitys").where(where).order("sorts DESC").limit(`${pageSize * (page - 1)}, ${pageSize}`).end();
    let conutSql = createSql.select('count(*) as count').from("my_abilitys").where(where).end();
    let result = await Db.query(sql, res);
    let count = await Db.query(conutSql, res);
    if (result.code || count.code) return;
    return res.json({
        ...successCode[200],
        data: {
            data: result,
            count: count[0].count
        },
    })
})

/**
 * @swagger
 * /api/admin/delAbility:
 *    post:
 *      tags:
 *      - 后台管理 我的能力
 *      summary: 我的能力-删除
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: 能力ID
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/delAbility", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    let { ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    if (isLogin.code > 200) return;
    let sql = createSql.update("my_abilitys").set({ is_del: 1, updatetime: utils.getTime() }).where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/exportAbility:
 *    post:
 *      tags:
 *      - 后台管理 我的能力
 *      summary: 我的能力-导出
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/exportAbility", async (req, res) => {
    let sql = createSql.select().from("my_abilitys").where(`is_del != 1`).order("sorts DESC").end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    let conf = {
        cols: [],
        rows: []
    };
    result.forEach(v => {
        v.addtime = utils.getTime(v.addtime);
        v.updateTime = v.updateTime ? utils.getTime(v.updateTime) : "";
        delete v.is_show;
        let arr = [v.ID, v.ability, v.sorts, v.updateTime, v.addtime];
        conf.rows.push(arr);
    })
    conf.cols = [
        { caption: 'ID', type: 'string' },
        { caption: '能力', type: 'string' },
        { caption: '排序', type: 'number' },
        { caption: '更新时间', type: 'string' },
        { caption: '添加时间', type: 'string' },
    ]
    let data = nodeExcel.execute(conf);
    res.setHeader('Content-Type', 'application/vnd.openxmlformats');
    res.setHeader("Content-Disposition", "attachment; filename=" + `myAbility-${new Date().getTime()}.xlsx`);
    res.end(data, 'binary');
})

/**
 * @swagger
 * /api/admin/addSchool:
 *    post:
 *      tags:
 *      - 后台管理 学校管理
 *      summary:  学校管理-添加
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                times:
 *                  type: string
 *                  description: 就读年限
 *                  required: true
 *                school:
 *                  type: string
 *                  description: 学校名称
 *                  required: true
 *                major:
 *                  type: string
 *                  description: 专业名称
 *                  required: true
 *                sorts:
 *                  type: number
 *                  description: 排序
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *                
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/addSchool", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let data = utils.checkData(req.body, objectCheck.school_add);
    if (data.code > 200) return res.json(data);
    data = data.data;
    if (!data.sorts) {
        data.sorts = 1;
    }
    data.ID = utils.getUuid();
    data.addtime = utils.getTime();
    let sql = createSql.insert("my_school").value(data).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200],
        data: {
            ID: data.ID
        }
    })
})

/**
 * @swagger
 * /api/admin/updateSchool:
 *    post:
 *      tags:
 *      - 后台管理 学校管理
 *      summary:  学校管理-更新
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                 type: string
 *                 description: ID
 *                 required: true
 *                times:
 *                  type: string
 *                  description: 就读年限
 *                school:
 *                  type: string
 *                  description: 学校名称
 *                major:
 *                  type: string
 *                  description: 专业名称
 *                sorts:
 *                  type: number
 *                  description: 排序
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *                
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/updateSchool", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    let data = {};
    if (!req.body.school) {
        data = utils.checkData(req.body, objectCheck.school_add);
        if (data.code > 200) return res.json(data);
        data = data.data;
    } else {
        data = req.body;
    }
    let sql = "";
    sql = createSql.update("my_school").set(data).where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/getSchool:
 *    get:
 *      tags:
 *      - 后台管理 学校管理
 *      summary: 学校管理-列表查询
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: pageSize
 *          default: 10
 *          description: 条数
 *          in: query
 *          required: false
 *          type: string
 *        - name: page
 *          default: 1
 *          description: 页码
 *          in: query
 *          required: false
 *          type: string
 *        - name: school
 *          description: 学校名称
 *          in: query
 *          required: false
 *          type: string
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getSchool", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { pageSize, page, school } = req.query;
    pageSize = pageSize || 10;
    page = page || 1;
    let where = "";
    if (school) {
        where = ` school LIKE '%${school}%' `
    }
    where += where ? ` AND is_del != 1 ` : ` is_del != 1 `;
    let sql = createSql.select().from("my_school").where(where).order("sorts DESC").limit(`${pageSize * (page - 1)}, ${pageSize}`).end();
    let conutSql = createSql.select('count(*) as count').from("my_school").where(where).end();
    let result = await Db.query(sql, res);
    let count = await Db.query(conutSql, res);
    if (result.code || count.code) return;
    result.forEach(item => {
        item.addtime = item.addtime ? utils.getTime(item.addtime) : null;
        item.updatetime = item.updatetime ? utils.getTime(item.updatetime) : null;
    })
    return res.json({
        ...successCode[200],
        data: {
            data: result,
            count: count[0].count
        },
    })
})

/**
 * @swagger
 * /api/admin/delAbility:
 *    post:
 *      tags:
 *      - 后台管理 学校管理
 *      summary: 学校管理-删除
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: 学校ID
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/delSchool", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    let sql = createSql.update("my_school").set({ is_del: 1, updatetime: utils.getTime() }).where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/addMyProject:
 *    post:
 *      tags:
 *      - 后台管理 项目经历
 *      summary:  项目经历-添加
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                title:
 *                  type: string
 *                  description: 标题
 *                  required: true
 *                time:
 *                  type: string
 *                  description: 项目时间
 *                  required: true
 *                introduce:
 *                  type: string
 *                  description: 项目介绍
 *                  required: true
 *                duty:
 *                  type: string
 *                  description: 职责 / 负责内容
 *                  required: true
 *                technology:
 *                  type: string
 *                  description: 技术栈
 *                  required: true
 *                sorts:
 *                  type: number
 *                  description: 排序
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *                
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/addMyProject", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let data = utils.checkData(req.body, objectCheck.myProject_add);
    if (data.code > 200) return res.json(data);
    data = data.data;
    if (!data.sorts) {
        data.sorts = 1;
    }
    data.ID = utils.getUuid();
    data.addtime = utils.getTime();
    let sql = createSql.insert("my_project").value(data).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200],
        data: {
            ID: data.ID
        }
    })
})

/**
 * @swagger
 * /api/admin/updateMyProject:
 *    post:
 *      tags:
 *      - 后台管理 项目经历
 *      summary:  项目经历-更新
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: ID
 *                  required: true
 *                title:
 *                  type: string
 *                  description: 标题
 *                time:
 *                  type: string
 *                  description: 项目时间
 *                introduce:
 *                  type: string
 *                  description: 项目介绍
 *                duty:
 *                  type: string
 *                  description: 职责 / 负责内容
 *                technology:
 *                  type: string
 *                  description: 技术栈
 *                sorts:
 *                  type: number
 *                  description: 排序
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *                
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/updateMyProject", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    let data = {};
    if (!req.body.title) {
        data = utils.checkData(req.body, objectCheck.myProject_add);
        if (data.code > 200) return res.json(data);
        data = data.data;
    } else {
        data = req.body;
    }
    let sql = "";
    sql = createSql.update("my_project").set(data).where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/getMyProject:
 *    get:
 *      tags:
 *      - 后台管理 项目经历
 *      summary: 项目经历-列表查询
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: pageSize
 *          default: 10
 *          description: 条数
 *          in: query
 *          required: false
 *          type: string
 *        - name: page
 *          default: 1
 *          description: 页码
 *          in: query
 *          required: false
 *          type: string
 *        - name: title
 *          description: 标题
 *          in: query
 *          required: false
 *          type: string
 *        - name: introduce
 *          description: 项目介绍
 *          in: query
 *          required: false
 *          type: string
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getMyProject", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { pageSize, page, title, introduce } = req.query;
    pageSize = pageSize || 10;
    page = page || 1;
    let where = "";
    if (title) {
        where = ` title LIKE '%${title}%' `
    }
    if (introduce) {
        where = where ? where + ` AND introduce LIKE '%${introduce}%' ` : ` introduce LIKE '%${introduce}%' `;
    }
    where += where ? ` AND is_del != 1 ` : ` is_del != 1 `;
    let sql = createSql.select().from("my_project").where(where).order("sorts DESC").limit(`${pageSize * (page - 1)}, ${pageSize}`).end();
    let conutSql = createSql.select('count(*) as count').from("my_project").where(where).end();
    let result = await Db.query(sql, res);
    let count = await Db.query(conutSql, res);
    if (result.code || count.code) return;
    result.forEach(item => {
        item.addtime = item.addtime ? utils.getTime(item.addtime) : null;
        item.updatetime = item.updatetime ? utils.getTime(item.updatetime) : null;
    })
    return res.json({
        ...successCode[200],
        data: {
            data: result,
            count: count[0].count
        },
    })
})

/**
 * @swagger
 * /api/admin/delMyProject:
 *    post:
 *      tags:
 *      - 后台管理 项目经历
 *      summary: 项目经历-删除
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: 学校ID
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/delMyProject", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    let sql = createSql.update("my_project").set({ is_del: 1, updatetime: utils.getTime() }).where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})


/**
 * @swagger
 * /api/admin/getUser:
 *    get:
 *      tags:
 *      - 后台管理 普通用户
 *      summary: 普通用户-列表查询
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: pageSize
 *          default: 10
 *          description: 条数
 *          in: query
 *          required: false
 *          type: string
 *        - name: page
 *          default: 1
 *          description: 页码
 *          in: query
 *          required: false
 *          type: string
 *        - name: username
 *          description: 用户名
 *          in: query
 *          required: false
 *          type: string
 *        - name: email
 *          description: 邮箱
 *          in: query
 *          required: false
 *          type: string
 *        - name: tel
 *          description: 手机号
 *          in: query
 *          required: false
 *          type: string
 *        - name: admin_user_id
 *          description: 管理员ID
 *          in: query
 *          required: false
 *          type: string
 *      responses:
 *        200:
 *         description: successful operation
 * */

router.get("/getUser", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;

    let { pageSize, page, username, email, tel, admin_user_id } = req.query;
    pageSize = pageSize || 10;
    page = page || 1;
    let where = "";
    if (username) {
        where = ` username LIKE '%${username}%' `
    }
    if (email) {
        where = where ? where + ` AND email LIKE '%${email}%' ` : ` email LIKE '%${email}%' `;
    }
    if (tel) {
        where = where ? where + ` AND tel LIKE '%${tel}%' ` : ` tel LIKE '%${tel}%' `;
    }
    if (admin_user_id) {
        where = where ? where + ` AND admin_user_id = '${admin_user_id}' ` : ` admin_user_id = '${admin_user_id}' `;
    }
    where += where ? ` AND is_del != 1 ` : ` is_del != 1 `;
    let sql = createSql.select().from("web_userinfo").where(where).order("addtime DESC").limit(`${pageSize * (page - 1)}, ${pageSize}`).end();
    let conutSql = createSql.select('count(*) as count').from("web_userinfo").where(where).end();
    let result = await Db.query(sql, res);
    let count = await Db.query(conutSql, res);
    if (result.code || count.code) return;

    result.forEach(item => {
        item.addtime = item.addtime ? utils.getTime(item.addtime) : null;
        item.updatetime = item.updatetime ? utils.getTime(item.updatetime) : null;
        if (item.icon) {
            let imgUrlArr = item.icon.split("/");
            let pathArr = imgUrlArr.filter(item => { return item == "https:" || item == "http:" });
            if (!pathArr.length) {
                item.icon = global.ip + item.icon;
            }
        }
        item.types = item.openid ? '微信小程序' : '网页端'
    })
    return res.json({
        ...successCode[200],
        data: {
            data: result,
            count: count[0].count
        },
    })
})

/**
 * @swagger
 * /api/admin/addUser:
 *    post:
 *      tags:
 *      - 后台管理 普通用户
 *      summary:  普通用户-添加
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                username:
 *                  type: string
 *                  description: 用户名
 *                  required: true
 *                password:
 *                  type: string
 *                  description: 密码
 *                icon:
 *                  type: string
 *                  description: 头像
 *                email:
 *                  type: string
 *                  description: 邮箱
 *                  required: true
 *                types:
 *                  type: string
 *                  description: 用户类型
 *                tel:
 *                  type: number
 *                  description: 手机号
 *                openid:
 *                  type: number
 *                  description: 微信openid
 *                admin_user_id:
 *                  type: string
 *                  description: 管理员ID
 *                is_stop:
 *                  type: number
 *                  description: 是否禁用
 *                
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/addUser", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let data = utils.checkData(req.body, objectCheck.user_add);
    if (data.code > 200) return res.json(data);
    data = data.data;
    if (!data.types) {
        data.types = 1;
    }
    data.ID = utils.getUuid();
    data.addtime = utils.getTime();
    data.password = data.password ? utils.MD5(data.password) : utils.MD5("123456");
    let sql = createSql.insert("web_userinfo").value(data).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200],
        data: {
            ID: data.ID
        }
    })
})

/**
 * @swagger
 * /api/admin/updataUser:
 *    post:
 *      tags:
 *      - 后台管理 普通用户
 *      summary:  普通用户-更新与重置密码
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: ID
 *                  required: true
 *                username:
 *                  type: string
 *                  description: 用户名
 *                  required: true
 *                password:
 *                  type: string
 *                  description: 密码
 *                icon:
 *                  type: string
 *                  description: 头像
 *                email:
 *                  type: string
 *                  description: 邮箱
 *                  required: true
 *                types:
 *                  type: string
 *                  description: 用户类型
 *                tel:
 *                  type: number
 *                  description: 手机号
 *                openid:
 *                  type: number
 *                  description: 微信openid
 *                admin_user_id:
 *                  type: string
 *                  description: 管理员ID
 *                is_stop:
 *                  type: number
 *                  description: 是否禁用
 *                reset:
 *                  type: boolean
 *                  description: 是否重置密码
 *                
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/updataUser", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    let data = {};
    if (req.body.username) {
        data = utils.checkData(req.body, objectCheck.user_add);
        if (data.code > 200) return res.json(data);
        data = data.data;
    } else {
        data = req.body;
    }
    if (data.reset) {
        data.password = utils.MD5({ word: "123456" });
        delete data.reset;
    }
    data.updatetime = utils.getTime();
    let sql = createSql.update("web_userinfo").set(data).where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/delUser:
 *    post:
 *      tags:
 *      - 后台管理 普通用户
 *      summary: 普通用户-删除
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: 学校ID
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/delUser", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    let sql = createSql.update("web_userinfo").set({ is_del: 1, updatetime: utils.getTime() }).where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})


/**
 * @swagger
 * /api/admin/getAdminUser:
 *    get:
 *      tags:
 *      - 后台管理 后台用户
 *      summary: 后台用户-列表查询
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: pageSize
 *          default: 10
 *          description: 条数
 *          in: query
 *          required: false
 *          type: string
 *        - name: page
 *          default: 1
 *          description: 页码
 *          in: query
 *          required: false
 *          type: string
 *        - name: username
 *          description: 用户名
 *          in: query
 *          required: false
 *          type: string
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getAdminUser", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { pageSize, page, username } = req.query;
    pageSize = pageSize || 10;
    page = page || 1;
    let where = "";
    if (username) {
        where = ` username LIKE '%${username}%' `
    }
    where += where ? ` AND is_del != 1 ` : ` is_del != 1 `;
    let sql = createSql.select().from("user_admin").where(where).order("addtime DESC").limit(`${pageSize * (page - 1)}, ${pageSize}`).end();
    let conutSql = createSql.select('count(*) as count').from("user_admin").where(where).end();
    let result = await Db.query(sql, res);
    let count = await Db.query(conutSql, res);
    if (result.code || count.code) return;
    return res.json({
        ...successCode[200],
        data: {
            data: result,
            count: count[0].count
        },
    })
})

/**
 * @swagger
 * /api/admin/updataAdminUser:
 *    post:
 *      tags:
 *      - 后台管理 后台用户
 *      summary:  后台用户-更新与重置密码
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: ID
 *                  required: true
 *                username:
 *                  type: string
 *                  description: 用户名
 *                  required: true
 *                password:
 *                  type: string
 *                  description: 密码
 *                power:
 *                  type: string
 *                  description: 权限ID
 *                reset:
 *                  type: boolean
 *                  description: 是否重置密码
 *                
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/updataAdminUser", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    let sql = createSql.select().from("user_admin").where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code > 200) return res.json(result);
    if (result[0].is_not_edit) {
        return res.json({
            ...errorCode[401]
        })
    }
    let data = {};
    data = req.body;
    if (data.reset) {
        data.password = utils.MD5({ word: "123456" });
        delete data.reset;
    }
    data.updatetime = utils.getTime();
    sql = createSql.update("user_admin").set(data).where(`ID = '${ID}'`).end();
    result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/delAdminUser:
 *    post:
 *      tags:
 *      - 后台管理 后台用户
 *      summary: 后台用户-删除
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: 学校ID
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/delAdminUser", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    let sql = createSql.select().from("user_admin").where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code > 200) return res.json(result);
    if (result[0].is_not_edit != 0) {
        return res.json({
            ...errorCode[401]
        })
    }
    sql = createSql.update("user_admin").set({ is_del: 1, updatetime: utils.getTime() }).where(`ID = '${ID}'`).end();
    result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/getRole:
 *    get:
 *      tags:
 *      - 后台管理 角色管理
 *      summary: 角色管理-列表查询
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: pageSize
 *          default: 10
 *          description: 条数
 *          in: query
 *          required: false
 *          type: string
 *        - name: page
 *          default: 1
 *          description: 页码
 *          in: query
 *          required: false
 *          type: string
 *        - name: title
 *          description: 角色名称
 *          in: query
 *          required: false
 *          type: string
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getRole", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { pageSize, page, title } = req.query;
    pageSize = pageSize || 10;
    page = page || 1;
    let where = "";
    if (title) {
        where = ` title LIKE '%${title}%' `
    }
    where += where ? ` AND is_del != 1 ` : ` is_del != 1 `;
    let sql = createSql.select().from("admin_user_roles").where(where).order("addtime DESC").limit(`${pageSize * (page - 1)}, ${pageSize}`).end();
    let conutSql = createSql.select('count(*) as count').from("admin_user_roles").where(where).end();
    let result = await Db.query(sql, res);
    let count = await Db.query(conutSql, res);
    if (result.code || count.code) return;
    return res.json({
        ...successCode[200],
        data: {
            data: result,
            count: count[0].count
        },
    })
})

/**
 * @swagger
 * /api/admin/addRole:
 *    post:
 *      tags:
 *      - 后台管理 角色管理
 *      summary:  角色管理-添加
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                title:
 *                  type: string
 *                  description: 角色名称
 *                  required: true
 *                role:
 *                  type: string
 *                  description: 角色菜单权限
 *                jurisdiction:
 *                  type: string
 *                  description: 角色操作权限
 *                is_not_edit:
 *                  type: string
 *                  description: 是否不可编辑
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/addRole", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let data = utils.checkData(req.body, objectCheck.role_add);
    if (data.code > 200) return res.json(data);
    let sql = createSql.select().from("admin_user_roles").where(`title = '${data.data.title}' AND is_del != 1`).end();
    let result = await Db.query(sql, res);
    if (result.code > 200) return res.json(result);
    if (result.length) {
        return res.json({
            ...errorCode[10006],
            msg: "角色名称已存在"
        })
    }
    data = data.data;
    data.ID = utils.getUuid();
    data.addtime = utils.getTime();
    sql = createSql.insert("user_role").value(data).end();
    result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200],
        data: {
            ID: data.ID
        }
    })
})


/**
 * @swagger
 * /api/admin/updataRole:
 *    post:
 *      tags:
 *      - 后台管理 角色管理
 *      summary:  角色管理-更新
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: ID
 *                  required: true
 *                title:
 *                  type: string
 *                  description: 角色名称
 *                  required: true
 *                role:
 *                  type: string
 *                  description: 角色菜单权限
 *                jurisdiction:
 *                  type: string
 *                  description: 角色操作权限
 *                is_not_edit:
 *                  type: string
 *                  description: 是否不可编辑
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/updataRole", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    let sql = createSql.select().from("admin_user_roles").where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code > 200) return res.json(result);
    if (result[0].is_not_edit) {
        return res.json({
            ...errorCode[401]
        })
    }
    let data = utils.checkData(req.body, objectCheck.role_add);
    if (data.code > 200) return res.json(data);
    data = data.data;
    if (data.title) {
        sql = createSql.select().from("admin_user_roles").where(`title = '${data.title}' AND is_del != 1 AND ID != '${ID}'`).end();
        result = await Db.query(sql, res);
        if (result.code > 200) return res.json(result);
        if (result.length) {
            return res.json({
                ...errorCode[10006],
                msg: "角色名称已存在"
            })
        }
    }
    data.updatetime = utils.getTime();
    sql = createSql.update("admin_user_roles").set(data).where(`ID = '${ID}'`).end();
    result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/delRole:
 *    post:
 *      tags:
 *      - 后台管理 角色管理
 *      summary: 角色管理-删除
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: 学校ID
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/delRole", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    let sql = createSql.select().from("admin_user_roles").where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code > 200) return res.json(result);
    if (result[0].is_not_edit) {
        return res.json({
            ...errorCode[401]
        })
    }
    sql = createSql.update("admin_user_roles").set({ is_del: 1, updatetime: utils.getTime() }).where(`ID = '${ID}'`).end();
    result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/getLog:
 *    get:
 *      tags:
 *      - 后台管理 日志管理
 *      summary: 日志管理-列表查询
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: pageSize
 *          default: 10
 *          description: 条数
 *          in: query
 *          required: false
 *          type: string
 *        - name: page
 *          default: 1
 *          description: 页码
 *          in: query
 *          required: false
 *          type: string
 *        - name: requestUrl
 *          description: 请求地址
 *          in: query
 *          required: false
 *          type: string
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getLog", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { pageSize, page, requestUrl } = req.query;
    pageSize = pageSize || 10;
    page = page || 1;
    let where = "";
    let sql = "";
    let conutSql = "";
    if (requestUrl) {
        where = ` requestUrl LIKE '%${requestUrl}%' `;
        sql = createSql.select().from("actionlog").where(where).order("createdTime DESC").limit(`${pageSize * (page - 1)}, ${pageSize}`).end();
        conutSql = createSql.select('count(*) as count').from("actionlog").where(where).end();
    } else {
        sql = createSql.select().from("actionlog").order("createdTime DESC").limit(`${pageSize * (page - 1)}, ${pageSize}`).end();
        conutSql = createSql.select('count(*) as count').from("actionlog").end();
    }
    let result = await Db.query(sql, res);
    let count = await Db.query(conutSql, res);
    if (result.code || count.code) return;
    result = utils.formatDateByArray(result, ["createdTime"])
    return res.json({
        ...successCode[200],
        data: {
            data: result,
            count: count[0].count
        },
    })
})

/**
 * @swagger
 * /api/admin/clearLog:
 *    post:
 *      tags:
 *      - 后台管理 日志管理
 *      summary: 日志管理-清空
 *      produces:
 *      - application/json
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.post("/clearLog", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let sql = createSql.customAll("TRUNCATE TABLE actionlog").build();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/getTopImg:
 *    get:
 *      tags:
 *      - 后台管理 顶部图管理
 *      summary: 顶部图管理-列表查询
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: pageSize
 *          default: 10
 *          description: 条数
 *          in: query
 *          required: false
 *          type: string
 *        - name: page
 *          default: 1
 *          description: 页码
 *          in: query
 *          required: false
 *          type: string
 *        - name: top_img_name
 *          description: 图片名称
 *          in: query
 *          required: false
 *          type: string
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getTopImg", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { pageSize, page, top_img_name } = req.query;
    pageSize = pageSize || 10;
    page = page || 1;
    let where = "";
    if (top_img_name) {
        where = ` top_img_name LIKE '%${top_img_name}%' `
    }
    where += where ? ` AND is_del != 1 ` : ` is_del != 1 `;
    let sql = createSql.select().from("top_img").where(where).order("addtime DESC").limit(`${pageSize * (page - 1)}, ${pageSize}`).end();
    let conutSql = createSql.select('count(*) as count').from("top_img").where(where).end();
    let result = await Db.query(sql, res);
    let count = await Db.query(conutSql, res);
    if (result.code || count.code) return;
    result.forEach(item => {
        item.addtime = item.addtime ? utils.getTime('yyyy-MM-dd HH:mm:ss', item.addtime) : null;
        item.updatetime = item.updatetime ? utils.getTime('yyyy-MM-dd HH:mm:ss', item.updatetime) : null;
        item.image_url = global.ip + item.image_url;
    })
    return res.json({
        ...successCode[200],
        data: {
            data: result,
            count: count[0].count
        },
    })
})

/**
 * @swagger
 * /api/admin/addTopImg:
 *    post:
 *      tags:
 *      - 后台管理 顶部图管理
 *      summary:  顶部图管理-添加
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                top_img_name:
 *                  type: string
 *                  description: 图片名称
 *                  required: true
 *                image_url:
 *                  type: string
 *                  description: 图片地址
 *                  required: true
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/addTopImg", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let data = utils.checkData(req.body, objectCheck.top_img_add);
    if (data.code > 200) return res.json(data);
    data = data.data;
    let sql = "";
    let result = null;
    // 只能显示一张
    if (data.is_show) {
        sql = createSql.update("top_img").set({ is_show: 0, updatetime: utils.getTime() }).where(`is_del != 1`).end();
        result = await Db.query(sql, res);
        if (result.code) return;
    }
    data.ID = utils.getUuid();
    data.addtime = utils.getTime();
    sql = createSql.insert("top_img").value(data).end();
    result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200],
        data: {
            ID: data.ID
        }
    })
})

/**
 * @swagger
 * /api/admin/editTopImg:
 *    post:
 *      tags:
 *      - 后台管理 顶部图管理
 *      summary:  顶部图管理-更新
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: ID
 *                  required: true
 *                top_img_name:
 *                  type: string
 *                  description: 图片名称
 *                  required: true
 *                image_url:
 *                  type: string
 *                  description: 图片地址
 *                  required: true
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/editTopImg", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let data = isLogin.data;
    if (data.top_img_name) {
        data = utils.checkData(req.body, objectCheck.top_img_add);
        if (data.code > 200) return res.json(data);
        data = data.data;
    } else {
        data = req.body;
    }
    let sql = "";
    let result = null;
    if (data.is_show) {
        sql = createSql.update("top_img").set({ is_show: 0, updatetime: utils.getTime() }).where(`is_del != 1`).end();
        result = await Db.query(sql, res);
        if (result.code) return;
    }
    data.updatetime = utils.getTime();
    sql = createSql.update("top_img").set(data).where(`ID = '${data.ID}'`).end();
    result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/delTopImg:
 *    post:
 *      tags:
 *      - 后台管理 顶部图管理
 *      summary: 顶部图管理-删除
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: 学校ID
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/delTopImg", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    let sql = createSql.update("top_img").set({ is_del: 1, updatetime: utils.getTime() }).where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/getProtocol:
 *    get:
 *      tags:
 *      - 后台管理 协议管理
 *      summary: 协议管理-列表查询
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: pageSize
 *          default: 10
 *          description: 条数
 *          in: query
 *          required: false
 *          type: string
 *        - name: page
 *          default: 1
 *          description: 页码
 *          in: query
 *          required: false
 *          type: string
 *        - name: title
 *          description: 协议名称
 *          in: query
 *          required: false
 *          type: string
 *        - name: type
 *          description: 协议类型
 *          in: query
 *          required: false
 *          type: string
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getProtocol", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { pageSize, page, title, type } = req.query;
    pageSize = pageSize || 10;
    page = page || 1;
    let where = "";
    if (title) {
        where = ` title LIKE '%${title}%' `
    }
    if (type) {
        where += where ? ` AND type = '${type}' ` : ` type = '${type}' `;
    }
    where += where ? ` AND is_del != 1 ` : ` is_del != 1 `;
    let sql = createSql.select().from("protocol").where(where).order("addtime DESC").limit(`${pageSize * (page - 1)}, ${pageSize}`).end();
    let conutSql = createSql.select('count(*) as count').from("protocol").where(where).end();
    let result = await Db.query(sql, res);
    let count = await Db.query(conutSql, res);
    if (result.code || count.code) return;
    result.forEach(item => {
        item.addtime = item.addtime ? utils.getTime('yyyy-MM-dd HH:mm:ss', item.addtime) : null;
        item.updatetime = item.updatetime ? utils.getTime('yyyy-MM-dd HH:mm:ss', item.updatetime) : null;
        item.content = decodeURIComponent(item.content) || "";
    })
    return res.json({
        ...successCode[200],
        data: {
            data: result,
            count: count[0].count
        },
    })
})

/**
 * @swagger
 * /api/admin/addProtocol:
 *    post:
 *      tags:
 *      - 后台管理 协议管理
 *      summary:  协议管理-添加
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                title:
 *                  type: string
 *                  description: 协议名称
 *                  required: true
 *                type:
 *                  type: string
 *                  description: 协议类型
 *                  required: true
 *                content:
 *                  type: string
 *                  description: 协议内容
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/addProtocol", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    } catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let data = utils.checkData(req.body, objectCheck.protocol_add);
    if (data.code > 200) return res.json(data);
    data = data.data;
    data.content = encodeURIComponent(data.content) || "";
    data.ID = utils.getUuid();
    data.addtime = utils.getTime();
    data.createByUser = isLogin.data.ID;
    let sql = createSql.insert("protocol").value(data).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200],
    })
})

/**
 * @swagger
 * /api/admin/updateProtocol:
 *    post:
 *      tags:
 *      - 后台管理 协议管理
 *      summary:  协议管理-更新
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: ID
 *                  required: true
 *                title:
 *                  type: string
 *                  description: 协议名称
 *                type:
 *                  type: string
 *                  description: 协议类型
 *                content:
 *                  type: string
 *                  description: 协议内容
 *                is_show:
 *                  type: number
 *                  description: 是否显示
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/updateProtocol", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    }
    catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { ID , title} = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    let data = {};
    if(title){
        data = utils.checkData(req.body, objectCheck.protocol_add);
        data = data.data;
        if (data.code > 200) return res.json(data)
        data.content = data.content ? encodeURIComponent(data.content) : "";
    }else{
        data.is_show = req.body.is_show;
    }
    if (data.code > 200) return res.json(data);
    data.updatetime = utils.getTime();
    delete data.ID;
    let sql = createSql.update("protocol").set(data).where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

/**
 * @swagger
 * /api/admin/delProtocol:
 *    post:
 *      tags:
 *      - 后台管理 协议管理
 *      summary: 协议管理-删除
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                ID:
 *                  type: string
 *                  description: 学校ID
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/delProtocol", async (req, res) => {
    let isLogin = {};
    try {
        isLogin = await utils.isLogin(req, res);
    }
    catch (error) {
        isLogin = error;
    }
    if (isLogin.code > 200) return;
    let { ID } = req.body;
    if (!ID) {
        return res.json({
            ...errorCode[10003]
        })
    }
    let sql = createSql.update("protocol").set({ is_del: 1, updatetime: utils.getTime() }).where(`ID = '${ID}'`).end();
    let result = await Db.query(sql, res);
    if (result.code) return;
    return res.json({
        ...successCode[200]
    })
})

module.exports = router;